export default (defaultShare?) => {
	const shareInfo = ref(defaultShare);
	const setShareInfo = newShareInfo => {
		shareInfo.value = { ...shareInfo.value, ...newShareInfo };
	};
	const initPageUrl = state => {
		const { path = '', title = '', imageUrl = '' } = state || {};
		let pageUrl = path;
		if (getCurrentPages().length > 0 && !pageUrl) {
			const data: any = getCurrentPages()[getCurrentPages().length - 1]; // 获取当前页面地址
			// console.log('data',data);
			let options = '';
			console.log('data.options',data.options);
			if (data.options && Object.keys(data.options).length > 0) {
				Object.keys(data.options).forEach(key => {
					options += `${key}=${data.options[key]}&`;
				});
				// console.log('options',options);
			} // 当前页面是否携带参数，并获取参数
			pageUrl = options ? `${data.route}?${options}` : data.route; // 整合页面地址及页面参数
			console.log('pageUrl',pageUrl);
		}
		return { path: pageUrl, title, imageUrl };
	};
	onShareAppMessage(() => {
		const { path, title, imageUrl } = initPageUrl(shareInfo.value);
		return {
			title: title || '倍阅读学生端',
			path: path || '/pages/index/index', // 分享默认跳转到页面地址
			imageUrl: imageUrl || '', // 分享卡片默认展示的图片
			success: () => {
				uni.showToast({
					title: '分享成功',
					duration: 3000,
				});
			},
			fail: () => {
				uni.showToast({
					title: '分享失败',
					icon: 'none',
					duration: 3000,
				});
			},
		};
	});

	onShareTimeline(() => {
		const { path, title, imageUrl } = initPageUrl(shareInfo.value);
		return {
			title: title || '倍阅读学生端',
			path: path || '/pages/index/index', // 分享默认跳转到页面地址
			imageUrl: imageUrl || '', // 分享卡片默认展示的图片
			success: () => {
				uni.showToast({
					title: '分享成功',
					duration: 3000,
				});
			},
			fail: () => {
				uni.showToast({
					title: '分享失败',
					icon: 'none',
					duration: 3000,
				});
			},
		};
	});
	return { onShareAppMessage, onShareTimeline, shareInfo, setShareInfo };
};
